package cn.com.cifi.mars.util.json;

import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.constant.CodeEnum;

public class JsonResultRespUtils {

	private R r;

	private JsonResultRespUtils() {
		this.r = new R();
	}

	public static JsonResultRespUtils create() {
		return new JsonResultRespUtils();
	}

	public JsonResultRespUtils setCode(int code) {
		r.setCode(code);
		return this;
	}

	public JsonResultRespUtils setMessage(String message) {
		r.setMessage(message);
		return this;
	}

	public JsonResultRespUtils setData(Object data) {
		r.setData(data);
		return this;
	}

	public JsonResultRespUtils setDefault(CodeEnum tipsCodeEnum) {
		r.setCode(tipsCodeEnum.getCode());
		r.setMessage(tipsCodeEnum.getMessage());
		return this;
	}

	public JsonResultRespUtils setSuccess(boolean success) {
		setDefault(success ? CodeEnum.SUCCESS : CodeEnum.INTERNAL_ERROR);
		return this;
	}

	public R build() {
		return r;
	}

	public static R success() {
		return create().setDefault(CodeEnum.SUCCESS).build();
	}

	public static R successWithData(Object data) {
		return create().setDefault(CodeEnum.SUCCESS).setData(data).build();
	}

	public static R success(String message) {
		return create().setCode(CodeEnum.SUCCESS.getCode()).setMessage(message).build();
	}

	public static R failed() {
		return create().setDefault(CodeEnum.INTERNAL_ERROR).build();
	}

	public static R failed(String message) {
		return create().setCode(CodeEnum.INTERNAL_ERROR.getCode()).setMessage(message).build();
	}

	public static R internalError() {
		return create().setDefault(CodeEnum.INTERNAL_ERROR).build();
	}

	public static R create(int code, String message) {
		return create().setCode(code).setMessage(message).build();
	}

	public static R create(CodeEnum codeEnum) {
		return create().setCode(codeEnum.getCode()).setMessage(codeEnum.getMessage()).build();
	}

}
